package com.magic.gre.ui.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jkb.vcedittext.VerificationCodeEditText;
import com.magic.gre.R;
import com.magic.gre.base.activity.BaseMVPActivity;
import com.magic.gre.entity.UserBean;
import com.magic.gre.helper.Apphelper;
import com.magic.gre.mvp.contract.CodeContract;
import com.magic.gre.mvp.presenter.CodePresenterImpl;
import com.noname.lib_base_java.util.L;
import com.noname.lib_base_java.util.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;

/**
 * Author: Lmoumou
 * Date: 2019-01-10 15:50
 * 文件名: CodeActivity
 * 描述:
 */
public class CodeActivity extends BaseMVPActivity<CodePresenterImpl> implements CodeContract.View,
        VerificationCodeEditText.OnVerificationCodeChangedListener {

    @BindView(R.id.mVerificationCodeEditText)
    VerificationCodeEditText mVerificationCodeEditText;
    @BindView(R.id.hint_tv)
    TextView hintTv;
    @BindView(R.id.root_v)
    LinearLayout rootV;
    @BindView(R.id.code_tv)
    TextView codeTv;

    public static void startThis(Context context, int tag, String phone, String smsCode) {
        Intent intent = new Intent(context.getApplicationContext(), CodeActivity.class);
        intent.putExtra("tag", tag);
        intent.putExtra("phone", phone);
        intent.putExtra("smsCode", smsCode);
        context.startActivity(intent);
    }

    public static void startThis(Context context, int tag, String phone, String smsCode, String openId) {
        Intent intent = new Intent(context.getApplicationContext(), CodeActivity.class);
        intent.putExtra("tag", tag);
        intent.putExtra("phone", phone);
        intent.putExtra("smsCode", smsCode);
        intent.putExtra("openId", openId);
        context.startActivity(intent);
    }
    //忘记密码
    public static final int PWSD = 9999;
    //登陆
    public static final int LOGIN = 9998;
    //注册
    public static final int REGISTER = 9997;
    //绑定手机号
    public static final int BIND_PHONE = 9996;

    /*判断值*/
    private int tag;
    private String phone = "";
    /*提交参数map*/
    private Map<String, Object> parmsMap;
    private String code;
    /*极光id*/
    private String jiguangId="";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_code;
    }

    @Override
    protected CodePresenterImpl creatPresenter() {
        return new CodePresenterImpl(this);
    }

    @Override
    protected void netOverride() {

    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        jiguangId = JPushInterface.getRegistrationID(this);
        parmsMap = new HashMap<>();
        code = getIntent().getStringExtra("smsCode");
        mVerificationCodeEditText.setOnVerificationCodeChangedListener(this);
        tag = getIntent().getIntExtra("tag", 0);
        phone = getIntent().getStringExtra("phone");
        String dealWithPhone = Apphelper.phoneDealWith(phone);
        String hinStr = "验证码已发送至手机：" + dealWithPhone;
        int start = hinStr.indexOf(dealWithPhone);
        int end = start + dealWithPhone.length();
        SpannableString spannableString = new SpannableString(hinStr);
        spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(this, R.color.main_color)), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        hintTv.setText(spannableString);

        cutdown(60 * 1000);
    }

    @OnClick(value = {R.id.code_tv})
    protected void onClick(View view) {
        switch (view.getId()) {
            case R.id.code_tv:
                L.e("LLSSQQ", "tag->" + tag);
                if (tag == REGISTER) {
                    mPresenter.pSmsCode(phone, 1);
                } else if (tag == LOGIN) {
                    mPresenter.pSmsCode(phone, 2);
                } else if (tag == PWSD) {
                    mPresenter.pSmsCode(phone, 3);
                } else if (tag == BIND_PHONE) {
                    mPresenter.pSmsCode(phone, 0);
                }
                break;
        }
    }

    @Override
    public void onVerCodeChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void onInputCompleted(CharSequence s) {

        switch (tag) {
            case LOGIN:
                showLoadDialog();
                parmsMap.clear();
                parmsMap.put("phone", phone);
                parmsMap.put("deviceType", 0);
                parmsMap.put("securityCode", String.valueOf(s));
                parmsMap.put("jiguangId", jiguangId);
                mPresenter.pLogin(parmsMap);
                break;
            case REGISTER:
                showLoadDialog();
                parmsMap.clear();
                parmsMap.put("phone", phone);
                parmsMap.put("deviceType", 0);
                parmsMap.put("securityCode", String.valueOf(s));
                parmsMap.put("jiguangId", jiguangId);
                mPresenter.pRegister(parmsMap);
                break;
            case PWSD:
                if (!String.valueOf(s).equals(code)) {
                    ToastUtil.getInstance().showNormal(this, "验证码错误");
                    return;
                }
                ModefyPwsdActivity.startThis(this, phone);
                break;
            case BIND_PHONE:
                parmsMap.clear();
                parmsMap.put("phone", phone);
                parmsMap.put("authCode", String.valueOf(s));
                parmsMap.put("openId", getIntent().getStringExtra("openId"));
                parmsMap.put("jiguangId", jiguangId);
                parmsMap.put("deviceType", 0);
                mPresenter.pBindPhone(parmsMap);
                break;
        }
    }

    @Override
    public void vLogin(UserBean userBean) {
        closeKeyBord();
        if (userBean.getIsGuide() == 1) {
            GuideActivity.startThis(this);
        } else {
            MainActivity.startThis(this);
        }
    }

    @Override
    public void vRegister(UserBean userBean) {
        closeKeyBord();
        if (userBean.getIsGuide() == 1) {
            GuideActivity.startThis(this);
        } else {
            MainActivity.startThis(this);
        }
    }

    @Override
    public void vBindPhone(UserBean userBean) {
        closeKeyBord();
        if (userBean.getIsGuide() == 1) {
            GuideActivity.startThis(this);
        } else {
            MainActivity.startThis(this);
        }
    }

    @Override
    public void vSmsCode(String code) {
        this.code = code;
        hideLoadDialog();
        ToastUtil.getInstance().showNormal(this, code);
        cutdown(60 * 1000);
    }

    private Handler handler = new Handler();

    @SuppressLint("SetTextI18n")
    private void cutdown(final int delay) {
        if (delay <= 0) {
            codeTv.setText("点击获取验证码");
            codeTv.setEnabled(true);
        } else {
            if (delay % 1000 == 0) {
                codeTv.setText(String.valueOf(delay / 1000) + "秒后重新发送");
            }
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    cutdown(delay - 100);
                }
            }, 100);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }
}
